package com.inhim.pjhim.adapter.provider;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chaychan.adapter.BaseItemProvider;
import com.inhim.pjhim.R;
import com.inhim.pjhim.adapter.ReadingTwoAdapter;
import com.inhim.pjhim.app.MyApplication;
import com.inhim.pjhim.entity.BannerList;
import com.inhim.pjhim.view.BannerView;
import com.inhim.pjhim.view.CustomRoundAngleImageView;
import com.inhim.pjhim.view.DotIndicatorView;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.List;

/**
 * @author ChayChan
 * @description: 头部banner
 * @date 2018/3/22  14:48
 */

public class BannerItemProviderNew extends BaseItemProvider<BannerList, BaseViewHolder> {
    private Context context;
    private Drawable mIndicatorFocusDrawable, mIndicatorNormalDrawable;
    private BannerView mBanner;

    public BannerItemProviderNew(Context context) {
        this.context = context;
    }

    @Override
    public void convert(BaseViewHolder helper, final BannerList news, int i) {
        if (news.getData() == null) {
            //如果没有标题，则直接跳过
            return;
        }
        try {
            mIndicatorFocusDrawable = mContext.getResources().getDrawable(R.mipmap.indicator_focus);
            mIndicatorNormalDrawable = mContext.getResources().getDrawable(R.mipmap.indicator_normal);
            mBanner=helper.getView(R.id.bannerView);
            mBanner.update(news.getData());

            //加载图片
           /* mBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    //imageView铺满屏幕
                    imageView.setScaleType(ImageView.ScaleType.CENTER);
                    GlideUtils.displayFromUrl(path,imageView,mContext)
                            .with(mContext)
                            .load(path)
                            .transform(new GlideRoundTransform(mContext, 8))
                            .into(imageView);
                }
            });*/
            // 显示圆形指示器
            /*mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            //指示器居中
            mBanner.setIndicatorGravity(BannerConfig.CENTER);
            //轮播时间(单位:毫秒)
            mBanner.setDelayTime(4000);*/
            //图片源
            //mBanner.setImages(bannerImage());
            //banner切换动画效果
            //    mBanner.setPageTransformer(true, getPageTransformer());
            //启动(必须在最后)
            //mBanner.start();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public int viewType() {
        return ReadingTwoAdapter.BANNER;
    }

    @Override
    public int layout() {
        return R.layout.recyclerview_header;
    }

    /**
     * 5.初始化点的指示器
     */
    private void initDotIndicator(List<BannerList.Data> bannlist, MZBannerView mMZBanner) {
        // 获取广告的数量
        int count = bannlist.size();

        // 让点的位置在右边
        mMZBanner.setGravity(Gravity.BOTTOM);

        for (int i = 0; i < count; i++) {
            // 不断的往点的指示器添加圆点
            DotIndicatorView indicatorView = new DotIndicatorView(mContext);
            // 设置大小
            LinearLayout.LayoutParams params = new
                    LinearLayout.LayoutParams(dip2px(8), dip2px(8));
            // 设置左右间距
            params.leftMargin = params.rightMargin = dip2px(2);
            indicatorView.setLayoutParams(params);

            if (i == 0) {
                // 选中位置
                indicatorView.setDrawable(mIndicatorFocusDrawable);
            } else {
                // 未选中的
                indicatorView.setDrawable(mIndicatorNormalDrawable);
            }
            mMZBanner.addView(indicatorView);
        }
    }

    /**
     * 5.把dip转成px
     */
    private int dip2px(int dip) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dip, context.getResources().getDisplayMetrics());
    }


    class LocalImageHolderView implements MZViewHolder<BannerList.Data> {

        private CustomRoundAngleImageView imageView;

        @Override
        public View createView(Context context) {
            // 返回页面布局文件
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            imageView = view.findViewById(R.id.banner_image);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            return view;
        }

        @Override
        public void onBind(Context context, int position, BannerList.Data data) {
            // 数据绑定
            Glide.with(MyApplication.getContext()).load(data.getImgUrl()).into(imageView);
        }
    }
}
